package subumbo.comps.carousel {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.setTimeout;
	
	[SWF(width=1000)]
	public class Carousel extends Sprite {
		
		private var _radiusX:Number;
		private var _radiusY:Number;
		private var _angle:Number;
		private var _velocity:Number;
		private var _speed:Number;
		
		public function Carousel() {
			_radiusX = 200;
			_radiusY = 10;
			_angle = 0;
			_speed = 5;
			_velocity = 0;
			addEventListener(Event.ADDED_TO_STAGE, added);
		}
		
		private function added(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, added);
			addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			setTimeout(startup,2000);
		}
		
		private function startup():void {
			addEventListener(Event.ENTER_FRAME, render);
		}
		
		private function mouseMove(e:MouseEvent):void {
			_velocity = (_radiusX - mouseX) / _radiusX * _speed; 
		}
		
		private function render(e:Event):void  {
			var rad:Number;
			var i:int; var l:int = numChildren; var increment:Number = 360 / l;
			var item:Item;
			for(;i<l;++i) {
				item = Item(getChildAt(i));
				item.angle += _velocity;				
				rad = item.angle / 180 * Math.PI; 
				item.x = Math.sin(rad) * _radiusX + _radiusX;
				item.y = Math.cos(rad) * _radiusY + _radiusY;
				item.alpha = item.scaleX = item.scaleY = (item.y) / _radiusY;
			}
		}
		
		private function build(angle:Number=0):void {
			var rad:Number;
			var i:int; var l:int = numChildren; var increment:Number = 360 / l;
			var item:Item;
			for(;i<l;++i) {
				rad = angle / 180 * Math.PI; angle += increment;
				item = Item(getChildAt(i));
				item.angle = angle;
				item.x = Math.sin(rad) * _radiusX + _radiusX;
				item.y = Math.cos(rad) * _radiusY + _radiusY;
				item.alpha = item.scaleX = item.scaleY = (item.y) / _radiusY;
			}
		}
		
		public function addItem(item:Item):void {
			addChild(item);
			item.update(null);
			build();
		}
		
		public function removeItem():void {
			
		}
	}
}